package br.com.sgmf.dao;

import br.com.sgmf.model.Marca;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.List;

@Repository
public class MarcaDAO {

    @PersistenceContext
    private EntityManager manager;

    @Transactional
    public void save(Marca marca) {
        manager.persist(marca);
    }

    @Transactional
    public void update(Marca marca) {
        manager.merge(marca);
    }

    @Transactional
    public void delete(Marca marca) {
        manager.remove(marca);
    }

    public Marca findById(Integer id) {
        return manager.find(Marca.class, id);
    }

    @SuppressWarnings("unchecked")
    public List<Marca> findAll() {
        return manager.createQuery("SELECT m FROM Marca m").getResultList();
    }

}
